#include "JDNNZhanJiRecordLayer.h"
#include "Game/GameLib.h"
#include "CMD_Ox.h"

#define  TXT_SCORE_WIDTH 122.0

bool JDNNZhanJiRecordLayer::init()
{
	if (!Layer::init())
		return false;


	layoutMain = CSLoader::createNode("ZhanJiLayer.csb");
	addChild(layoutMain);

	auto btn = (Button*)layoutMain->getChildByName("Button_close");
	btn->addTouchEventListener(CC_CALLBACK_2(JDNNZhanJiRecordLayer::BtnCallBack, this));

	_listView = (ListView*)layoutMain->getChildByName("ListView_1");
	for (int i = 0; i < 10; i++)
	{
		m_TxtID[i] = (Text*)layoutMain->getChildByName(StringUtils::format("Text_%d",i));
		m_TxtID[i]->setString("");
		m_TxtAllScore[i] = (Text*)layoutMain->getChildByName(StringUtils::format("Text_%d_0", i));
		m_TxtAllScore[i]->setString("");
	}
	clearZhanJiRecord();
	
	return true;
}

void JDNNZhanJiRecordLayer::onEnter()
{
	Layer::onEnter();
}

void JDNNZhanJiRecordLayer::onExit()
{
	Layer::onExit();
}



void JDNNZhanJiRecordLayer::BtnCallBack(Ref* pSender, Widget::TouchEventType touchtype)
{
	Button* btn = (Button*)pSender;
	if (!btn) return;
	switch (touchtype)
	{
	case cocos2d::ui::Widget::TouchEventType::ENDED:
	{
		SoundFun::playEffect("effect_click.mp3");
		if (btn->getName().compare("Button_close") == 0)
		{
			hideZhanJiRecordLayer();
		}		
	}
		break;
	default:
		break;
	}
}


void JDNNZhanJiRecordLayer::showZhanJiRecordLayer()
{
	layoutMain->setPosition(0, 0);
}
void JDNNZhanJiRecordLayer::hideZhanJiRecordLayer()
{
	layoutMain->setPosition(0, 1100);
}

void JDNNZhanJiRecordLayer::createZhanJiRecord(const int index, const int playerNum, const LONGLONG* playerScore, const int localPlayerIndex)
{
	auto layout = Layout::create();
	layout->setContentSize(Size(1365, 67));

	std::string sIndex = StringUtils::format("%d", index + 1);
	Text* txtJvNum = Text::create(sIndex, "common/WRYH.ttf", 30);
	txtJvNum->setPosition(Vec2(69, 25));
	layout->addChild(txtJvNum);

	for (int i = 0; i < playerNum; i++)
	{
		std::string  score = StringUtils::format("%lld", playerScore[i]);
		Text* playerScore = Text::create(score, "common/WRYH.ttf", 30);
		playerScore->setPosition(Vec2(200 + i*TXT_SCORE_WIDTH, 28));
		if (i == localPlayerIndex)
		{
			playerScore->setTextColor(cocos2d::ccColor4B(183, 12, 1, 255));
		}

		layout->addChild(playerScore);
	}

	//_listView->addChild(layout);
	_listView->pushBackCustomItem(layout);
}


void JDNNZhanJiRecordLayer::showPlayerID(int index, int mId)
{
	m_TxtID[index]->setString(StringUtils::format("%d",mId));
}

void JDNNZhanJiRecordLayer::showPlayerAllScore(int index, int score, const int localPlayerIndex)
{
	m_TxtAllScore[index]->setString(StringUtils::format("%d", score));
	m_TxtAllScore[index]->setTextColor(cocos2d::ccColor4B(255, 255, 255, 255));

	if (index == localPlayerIndex)
	{
		m_TxtAllScore[index]->setTextColor(cocos2d::ccColor4B(183, 12, 1, 255));
	}
}

void JDNNZhanJiRecordLayer::clearZhanJiRecord()
{
	for (int i = 0; i < 10; i++)
	{
		m_TxtID[i]->setString("");
		m_TxtAllScore[i]->setString("");
	}

	_listView->removeAllChildrenWithCleanup(true);
}